package com.jc.order.service.impl;

import com.jc.order.common.OrderNumberGenerator;
import com.jc.order.domain.KjbInsuranceCross;
import com.jc.order.dto.JcPolicyDTO;
import com.jc.order.mapper.KjbInsuranceCrossMapper;
import com.jc.order.service.InsuranceCrossService;
import com.jc.order.service.KjbInsurancesService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class InsuranceCrossServiceImpl implements InsuranceCrossService {

    @Resource
    private KjbInsuranceCrossMapper kjbInsuranceCrossMapper;
    @Resource
    private KjbInsurancesService kjbInsurancesService;

    @Override
    @Transactional
    public JcPolicyDTO insertPolicy(JcPolicyDTO dto) {
        KjbInsuranceCross entity = new KjbInsuranceCross();
        dto.setSnNo(OrderNumberGenerator.generate());
        BeanUtils.copyProperties(dto, entity);
        //处理附件
//        this.getAttachment(entity);

        kjbInsuranceCrossMapper.insert(entity);
        dto.setId(entity.getId());
        kjbInsurancesService.saveInsurances(dto);
        return dto;
    }

    @Override
    @Transactional
    public JcPolicyDTO updatePolicy(JcPolicyDTO dto) {

        KjbInsuranceCross entity = new KjbInsuranceCross();
//        dto.setSnNo(OrderNumberGenerator.generate());
        BeanUtils.copyProperties(dto, entity);
//        this.getAttachment(entity);
        kjbInsuranceCrossMapper.updateByPrimaryKey(entity);
//        dto.setId(entity.getId());
        kjbInsurancesService.saveInsurances(dto);
        return dto;
    }

    @Override
    public JcPolicyDTO queryById(Long id) {
        JcPolicyDTO dto = null;
        KjbInsuranceCross entity = kjbInsuranceCrossMapper.selectByPrimaryKey(id);
        if (entity != null) {
            dto = new JcPolicyDTO();
            BeanUtils.copyProperties(dto, entity);
        }
        return dto;
    }


    @Override
    public JcPolicyDTO selectByOrderNumber(String orderNumber, Long tenantId) {
        JcPolicyDTO dto = null;
        Example example = new Example(KjbInsuranceCross.class, Boolean.TRUE, Boolean.TRUE);
        example.createCriteria().andEqualTo("orderNumber", orderNumber).andEqualTo("tenantId", tenantId);
        List<KjbInsuranceCross> traditionalList = kjbInsuranceCrossMapper.selectByExample(example);
        if (traditionalList != null && traditionalList.size() > 0) {
            dto = new JcPolicyDTO();
            BeanUtils.copyProperties(traditionalList.get(0), dto);
        }
        return dto;
    }

    @Override
    public JcPolicyDTO selectByOrderNumberAndSnNo(String orderNumber, String snNo, Long tenantId) {

        JcPolicyDTO dto = null;
        Example example = new Example(KjbInsuranceCross.class, Boolean.TRUE, Boolean.TRUE);
        example.createCriteria().andEqualTo("orderNumber", orderNumber)
                .andEqualTo("snNo", snNo)
                .andEqualTo("tenantId", tenantId);
        List<KjbInsuranceCross> traditionalList = kjbInsuranceCrossMapper.selectByExample(example);
        if (traditionalList != null && traditionalList.size() > 0) {
            dto = new JcPolicyDTO();
            BeanUtils.copyProperties(traditionalList.get(0), dto);
        }
        return dto;
    }

    @Override
    public List<JcPolicyDTO> selectList(List<String> snNos, Long tenantId) {
        if (tenantId == null || snNos == null || snNos.size() == 0) {
            return null;
        }
        List<JcPolicyDTO> list = null;
        Example example = new Example(KjbInsuranceCross.class, Boolean.TRUE, Boolean.TRUE);
        example.createCriteria().andIn("snNo", snNos).andEqualTo("tenantId", tenantId);
        List<KjbInsuranceCross> traditionalList = kjbInsuranceCrossMapper.selectByExample(example);
        if (traditionalList != null && traditionalList.size() > 0) {
            list = new ArrayList<>();
            for (int i = 0; i < traditionalList.size(); i++) {
                JcPolicyDTO dto = new JcPolicyDTO();
                KjbInsuranceCross kjbInsuranceCross = traditionalList.get(i);
                BeanUtils.copyProperties(kjbInsuranceCross, dto);
                dto.setRmbPremium(dto.getFee());
                list.add(dto);
            }
        }
        return list;
    }


//    private void getAttachment(KjbInsuranceCross entity) {
//
//        if (StrUtil.isNotEmpty(entity.getGoodsPackageImages())) {
//
//        }
//        if (StrUtil.isNotEmpty(entity.getExpressTrackingNumberFile())) {
//            String value = this.getJson("快递单号附件", entity.getExpressTrackingNumberFile());
//            if (StrUtil.isNotEmpty(value)){
//                entity.setExpressTrackingNumberFile(value);
//                entity.setExpressTrackingNumber("见附件");
//            }
//        }
//        if (StrUtil.isNotEmpty(entity.getGoodsNoteFile())) {
//            String value =   this.getJson("货物描述附件", entity.getGoodsNoteFile());
//            if (StrUtil.isNotEmpty(value)){
//                entity.setGoodsNoteFile(value);
//                entity.setGoodsNote("见附件");
//            }
//        }
//    }
//
//    private String getJson(String type, String fileUrl) {
//        JSONArray array = new JSONArray();
//        JSONObject object = new JSONObject();
//        //获取文件后缀 不含 .
//        String ext = FileNameUtil.extName(fileUrl);
//        if (StrUtil.isNotEmpty(ext)) {
//            object.put("originName", type + "." + ext);
//            object.put("name", fileUrl);
//            object.put("url", fileUrl);
//            object.put("uid", DateUtil.date().getTime());
//            object.put("status", "success");
//            array.add(object);
//            return array.toString();
//        }
//        return null;
//    }


}
